package com.champ.thread;

public class CanStopThread extends Thread {

	@Override
	public void run() {
		for (int i = 0; i < 5001000; i++) {

			if (this.interrupted()) {
				System.out.println("线程要停止了，马上退出！！" + this.getName() + this.hashCode());
				break;
			}
			System.out.println("i = " + (i + 1));
		}
		System.out.println("如果代码时for有继续运行，线程没有停止！！！ ");
		super.run();
	}

}

class CanStopThread2 extends Thread {

	@Override
	public void run() {
		try {
			for (int i = 0; i < 1010110; i++) {
				if (this.interrupted()) {
					System.out.println("线程已经停止，我要退出");
					throw new InterruptedException();
				}
				System.out.println("i = " + i);
			}
		} catch (InterruptedException e) {
			System.out.println("进入CanStopThread2的catch 块中");
			e.printStackTrace();
		}
		super.run();
	}

}

class ForcibleStopThread extends Thread {
	private int i = 0;

	@Override
	public void run() {
		try {
			while (true) {
				i++;
				System.out.println("i = " + i + 1);
				Thread.sleep(1000);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
			// TODO: handle exception
		}
	}
}

class ForcibleStopThreadToExp extends Thread {
	@Override
	public void run() {
		try {
			this.stop();
		} catch (ThreadDeath e) {
			System.out.println("进入了ForcibleStopThreadToExp的catch块中！！！");
			e.printStackTrace();
			// TODO: handle exception
		}
	}
}

class UseInterruptAndReturnStopThread extends Thread {
	@Override
	public void run() {
		super.run();
		while (true) {
			if (this.isInterrupted()) {
				System.out.println("线程停止了");
				return;
			}
			System.out.println("time is : " + System.currentTimeMillis());
		}
	}
}